#include "sys_app.h"
#include "sys_main.h"
#include "record.h"
#include "freertos/timers.h"

record_app_data_t* record_app_data = NULL;

record_app_data_t* record_get_data(void)
{
    return record_app_data;
}

int record_get_current_state(void)
{
    return record_app_data->record_state;
}

static void record_timer_callback(TimerHandle_t xTimer)
{
    if (!record_app_data) {
        return;
    }

    switch (record_app_data->record_state) {
        case RECORD_STATE_RECORDING:
            recorder_process_data(record_app_data);
            break;
        case RECORD_STATE_PAUSED:
            // 暂停状态不做处理
            break;
        default:
            break;
    }
}

static int record_init(void)
{
    ESP_LOGI(TAG, "%s", __func__);

    if (record_app_data) {
        ESP_LOGE(TAG, "%s: record_app_data is not NULL", __func__);
        return -1;
    }

    record_app_data = malloc(sizeof(record_app_data_t));
    if (record_app_data == NULL) {
        ESP_LOGE(TAG, "%s: malloc failed", __func__);
        goto exit;
    }

    memset(record_app_data, 0, sizeof(record_app_data_t));

    record_app_data->record_state = RECORD_STATE_IDLE;
    record_app_data->file_mutex = xSemaphoreCreateMutex();
    
    if (record_app_data->file_mutex == NULL) {
        ESP_LOGE(TAG, "%s: create file_mutex failed", __func__);
        goto exit;
    }

    record_app_data->record_timer = xTimerCreate(
        "record_timer",
        pdMS_TO_TICKS(10),
        pdTRUE,
        (void*)0,
        record_timer_callback);
    if (record_app_data->record_timer == NULL) {
        ESP_LOGE(TAG, "Failed to create record timer");
        goto exit;
    }

    // record_app_data->data_transfer_timer = xTimerCreate(
    //     "data_transfer_timer", 
    //     pdMS_TO_TICKS(100),
    //     pdTRUE,
    //     (void*)0,
    //     data_transfer_timer_callback);
    // if (record_app_data->data_transfer_timer == NULL) {
    //     ESP_LOGE(TAG, "Failed to create data transfer timer");
    //     xTimerDelete(record_app_data->record_timer, portMAX_DELAY);
    //     goto exit;
    // }

    record_view_init();

    recorder_meida_init(record_app_data);

    return 0;

exit:
    if (record_app_data->file_mutex) {
        vSemaphoreDelete(record_app_data->file_mutex);
    }
    if (record_app_data) {
        free(record_app_data);
        record_app_data = NULL;
    }

    return -1;
}

static int record_exit(void)
{
    ESP_LOGI(TAG, "%s", __func__);

    if (record_app_data) {
        // 删除定时器
        if (record_app_data->record_timer) {
            xTimerDelete(record_app_data->record_timer, portMAX_DELAY);
        }
        // if (record_app_data->data_transfer_timer) {
        //     xTimerDelete(record_app_data->data_transfer_timer, portMAX_DELAY);
        // }

        if (record_app_data->file_mutex) {
            vSemaphoreDelete(record_app_data->file_mutex);
        }

        record_iterator_deinit(record_app_data);

        recorder_meida_deinit(record_app_data);

        record_view_exit();

        free(record_app_data);
        record_app_data = NULL;
    }

    return 0;
}

void record_main_loop(void *arg)
{
    struct app_msg* msg = NULL;

    while (sys_get_cur_app() == SYS_APP_RECORD) {
        if (msg_manager_receive_msg(&msg, portMAX_DELAY)) {
            ESP_LOGI(TAG, "%s: receive msg type: %d, cmd: %d, value: %08x", __func__, msg->type, msg->cmd, msg->value);
            switch (msg->type) {
                case MSG_INIT_APP:
                    if (record_init()) {
                        sys_app_quit(sys_get_cur_app());
                    }
                break;

                case MSG_INPUT_EVENT:
				    record_input_event_proc(msg);
				break;

                case MSG_EXIT_APP:
                    record_exit();
                break;

                default:
                    ESP_LOGE(TAG, "%s: unknown msg type: %d", __func__, msg->type);
                break;
            }
            msg_manager_receive_exit(msg);
        }
    }
}
